package main

import (
    "context"

    "github.com/gogf/gf/v2/frame/g"
    "github.com/gogf/gf/v2/net/ghttp"
)

type HelloReq struct {
    g.Meta `path:"/" method:"get"`
    Name   string `v:"required" dc:"姓名"`
    Age    int    `v:"required" dc:"年龄"`
}

type HelloAdd struct {
    g.Meta `path:"/add" method:"get"`
    Name   string `v:"required" dc:"姓名"`
    Age    int    `v:"required" dc:"年龄"`
}
type HelloRes struct{}

type Hello struct{}

func (Hello) Say(ctx context.Context, req *HelloReq) (res *HelloRes, err error) {
    r := g.RequestFromCtx(ctx)
    r.Response.Writef(
        "Hello %s! Your Age is %d",
        req.Name,
        req.Age,
    )
    return
}

func (Hello) AgeAdd(ctx context.Context, req *HelloAdd) (res *HelloRes, err error) {
    r := g.RequestFromCtx(ctx)
    r.Response.Writef(
        "Hello %s! Your AddAge is %d",
        req.Name,
        req.Age + 1,
    )
    return
}

func ErrorHandler(r *ghttp.Request) {
    // 执行路由函数
    r.Middleware.Next()
    // 判断是否产生错误
    if err := r.GetError(); err != nil {
        r.Response.Write("error occurs: ", err.Error())
        return
    }
}

func main() {
    s := g.Server()
    s.Group("/", func(group *ghttp.RouterGroup) {
		group.Middleware(ErrorHandler)
        group.Bind(
            new(Hello),
        )
    })
    s.SetPort(8000)
    s.Run()
}